package com.lianbang.staff.module.user

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import cn.ubest.mvvmcore.utils.ToastUtils
import com.lianbang.staff.R
import com.lianbang.staff.databinding.ActivityLoginBinding
import com.lianbang.staff.module.main.MainActivity
import com.lianbang.staff.vm.MainVM
import com.lianbang.staff.vm.UserVM
import kotlinx.android.synthetic.main.activity_login.*

/**
 * Created by maoqi on 2020/12/28.
 */
class LoginActivity : BaseActivity<UserVM, ActivityLoginBinding>() {

    companion object {
        fun start(context: Context?) {
            context?.also {
                val starter = Intent(it, LoginActivity::class.java)
                if (it !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                it.startActivity(starter)
            }
        }
    }

    override fun initLayout(): Int {
        return R.layout.activity_login
    }

    override fun initData(intent: Intent) {
    }

    override fun initView() {
        btn_register.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                RegisterActivity.start(this@LoginActivity)
            }

        })
        btn_forget_password.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                FindPasswordActivity.start(this@LoginActivity)
            }
        })

        btn_login.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                val account = et_account.text.toString()
                val password = et_password.text.toString()
                if (account.isEmpty()) {
                    ToastUtils.toast("账号不能为空")
                    return
                }
                if (password.isEmpty()) {
                    ToastUtils.toast("密码不能为空")
                    return
                }
                mViewModel.login(account, password)
                    .observe(this@LoginActivity,getObserver {
                        ToastUtils.toast("登录成功")
                        MainActivity.start(this@LoginActivity,MainActivity.TAB_HOME)
                        finish()
                    })
            }
        })
    }

    override fun loadData() {

    }

}